#if !defined(RT_COM_EXCEPTION_H)
#define RT_COM_EXCEPTION_H

#include <tchar.h>
#include <winerror.h>
#include <wtypes.h>

#include <rt/Exceptions/Exception.h>

#define THR(expr_) rt::COMException::ThrowHResult(expr_, _T(#expr_), _T(__FILE__), __LINE__)

namespace rt
{
	class COMException : public Exception
	{
	public:
		explicit COMException(HRESULT status, LPCTSTR expr, LPCTSTR file, UINT line)
			: Exception("COM error"),
			m_status(status),
			m_expr(expr),
			m_file(file),
			m_line(line)
		{
		}
		HRESULT Status() const { return m_status; }
		LPCTSTR File() const { return m_file; }
		UINT Line() const { return m_line; }

		static HRESULT ThrowHResult(HRESULT status, LPCTSTR expr, LPCTSTR file, UINT line)
		{
			if (FAILED(status))
			{
				throw COMException(status, expr, file, line);
			}
			return status;
		}

	private:
		HRESULT m_status;
		LPCTSTR m_expr;
		LPCTSTR m_file;
		UINT m_line;
	};
}

#endif
